﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;


namespace AvvaMobileNHibernateHelperKit
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        Framework.Data dataClass = new Framework.Data();

        private void SetConnectionString()
        {
            dataClass.ConnectionString = "server=" + cmbServerNames.Text + ";database=" + cmbDatabaseNames.Text + ";user id=" + txtUsername.Text + ";password=" + txtPassword.Text + ";";
        }

        private void btnRefreshServerNames_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                cmbServerNames.DataSource = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources();
                cmbServerNames.DisplayMember = "ServerName";
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Server Names", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void rdoSqlAuthentication_CheckedChanged(object sender, EventArgs e)
        {
            pnlUserInfo.Enabled = (rdoSqlAuthentication.Checked) ? true : false;
        }

        private void comboBox1_DropDown(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                dataClass.ConnectionString = "server=" + cmbServerNames.Text + ";database=master;user id=" + txtUsername.Text + ";password=" + txtPassword.Text + ";";
                cmbDatabaseNames.DataSource = dataClass.GetDatabaseNames("select name, dbid from sysdatabases");
                cmbDatabaseNames.DisplayMember = "name";
                cmbDatabaseNames.ValueMember = "dbid";
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }

        private void btnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                SetConnectionString();
                dataClass.TestConnection();
                MessageBox.Show("Test connection succeeded", "Connection", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Connection", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            tabMain.SelectedIndex = 1;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                SetConnectionString();
                DataTable dt = dataClass.ExecuteReader("SELECT [TableName] = so.name, [ID] = so.id FROM sysobjects so WHERE OBJECTPROPERTY(so.id, 'IsMsShipped') = 0 AND so.xtype = 'U'");
                chklistTables.Items.Clear();
                foreach(DataRow dr in dt.Rows)
                {
                    chklistTables.Items.Add(dr["TableName"]);
                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Load Tables", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void btnLoadTables_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                SetConnectionString();
                DataTable dt = dataClass.ExecuteReader("SELECT [TableName] = so.name, [ID] = so.id FROM sysobjects so WHERE OBJECTPROPERTY(so.id, 'IsMsShipped') = 0 AND so.xtype = 'U'");
                chklistTables.Items.Clear();
                foreach(DataRow dr in dt.Rows)
                {
                    chklistTables.Items.Add(dr["TableName"]);
                }
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Load Tables", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                this.Cursor = Cursors.WaitCursor;
                SetConnectionString();
                DataTable dt = dataClass.GetTableColumnsInfo("EXEC dbo.sp_MShelpcolumns '" + chklistTables.SelectedItem.ToString() + "'");
                ColumnsPreview previewForm = new ColumnsPreview(dt);
                previewForm.ShowDialog();
                
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message, "Columns Preview", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                this.Cursor = Cursors.Default;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            tabMain.SelectedIndex = 2;
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:myilmaz@dotnetishere.com");
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.dotnetishere.com");
        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("mailto:myilmaz@avvamobile.com");
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.avvamobile.com");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.avvamobile.com");
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.dotnetishere.com");
        }       

        private void button3_Click(object sender, EventArgs e)
        {
            Generate();
        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            tabMain.SelectedIndex = 3;
        }

        private void Generate()
        {
            string status = "without";
            try
            {
                this.Cursor = Cursors.WaitCursor;
                Progress("Generating starting", 0);
                System.Threading.Thread.Sleep(2000);
                Progress("Generating started", 0);

                #region Check bin Directory Exist And Create
                    
                if(!System.IO.Directory.Exists(txtProjectFolder.Text + @"\bin"))
                {
                    System.IO.Directory.CreateDirectory(txtProjectFolder.Text + @"\bin");
                }

                if(!System.IO.Directory.Exists(txtProjectFolder.Text + @"\App_Code"))
                {
                    System.IO.Directory.CreateDirectory(txtProjectFolder.Text + @"\App_Code");
                }

                if(!System.IO.Directory.Exists(txtProjectFolder.Text + @"\App_Code\NHMappings"))
                {
                    System.IO.Directory.CreateDirectory(txtProjectFolder.Text + @"\App_Code\NHMappings");
                }

                #endregion

                #region Copy Files(Assemblies)
                Progress("Assemblies copying", 5);
                string projectPath = @"c:\program files\Avva Mobile Enterprise Solutions\NHibernate Helper Kit";

                if(chkCopyNhibernateDll.Checked){
                    System.IO.File.Copy(projectPath + @"\NHibernate.dll", txtProjectFolder.Text + @"\bin\NHibernate.dll", true);
                    Progress("NHibernate assembly copied", 0);
                }

                if(chkCopyLog4netDll.Checked){
                    System.IO.File.Copy(projectPath + @"\log4net.dll", txtProjectFolder.Text + @"\bin\log4net.dll", true);
                    Progress("Log4net assembly copied", 0);
                }

                if(chkCopyNullablesDll.Checked){
                    System.IO.File.Copy(projectPath + @"\Nullables.dll", txtProjectFolder.Text + @"\bin\Nullables.dll", true);
                    Progress("Nullables assembly copied", 0);
                }
                Progress("Selected assemblies copied", 10);
                #endregion

                #region Write NHibernate Config File
                
                Progress("NHibernate config file writing", 15);
                System.IO.StreamWriter sw = new System.IO.StreamWriter(txtProjectFolder.Text + @"\bin\hibernate.cfg.xml");
                sw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
                sw.WriteLine("<hibernate-configuration xmlns=\"urn:nhibernate-configuration-2.0\">");
                sw.WriteLine("<session-factory>");
                sw.WriteLine("<property name=\"connection.provider\">NHibernate.Connection.DriverConnectionProvider</property>");
                sw.WriteLine("<property name=\"connection.driver_class\">NHibernate.Driver.SqlClientDriver</property>");
                sw.WriteLine("<property name=\"connection.connection_string\">Server=" + cmbServerNames.Text + ";initial catalog=" + cmbDatabaseNames.Text + ";User Id=" + txtUsername.Text + ";Password=" + txtPassword.Text + ";</property>");
                sw.WriteLine("<property name=\"show_sql\">true</property>");
                sw.WriteLine("<property name=\"dialect\">NHibernate.Dialect.MsSql2000Dialect</property>");
                sw.WriteLine("<property name=\"use_outer_join\">true</property>");
                sw.WriteLine("<mapping assembly=\"" + txtAssemblyName.Text + "\" />");
                sw.WriteLine("</session-factory>");
                sw.WriteLine("</hibernate-configuration>");               
                sw.Flush();
                sw.Close();
                sw.Dispose();

                Progress("NHibernate config file generated", 20);

                #endregion

                Progress("Setting connection", 25);
                SetConnectionString();
                Progress("Connection setted", 30);

                if(rdoCSharp.Checked)
                {
                    CSharp_MsSql();
                }
                else if (rdoVBNET.Checked)
                {
                    VBNet_MsSql();
                }

                #region Write XML(*.hbm.xml)Config Files
                
                Progress("Config files writing", 70);

                for (int i = 0; i <= chklistTables.Items.Count - 1; i++)
                {
                    if (chklistTables.GetItemCheckState(i) == CheckState.Checked)
                    {
                        string tableName = chklistTables.Items[i].ToString();
                        System.IO.StreamWriter sw3 = new System.IO.StreamWriter(txtProjectFolder.Text + @"\App_Code\NHMappings\" + tableName + ".hbm.xml");
                        DataTable dt = dataClass.GetTableColumnsInfo("EXEC dbo.sp_MShelpcolumns '" + tableName + "'");

                        sw3.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
                        sw3.WriteLine("<hibernate-mapping xmlns=\"urn:nhibernate-mapping-2.0\">");

                        string dynamicUpdate = "";
                        if (chkDynamicUpdate.Checked) { dynamicUpdate = "dynamic-update=\"true\""; }
                        sw3.WriteLine("<class name=\"" + txtDefaultNamespace.Text + "." + tableName + ", " + txtAssemblyName.Text + "\" table=\"" + tableName + "\" " + dynamicUpdate + ">");

                        foreach (DataRow dr in dt.Rows)
                        {
                            if (Convert.ToInt32(dr["col_identity"]) == 1)
                            {
                                sw3.WriteLine("<id name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Int32\" unsaved-value=\"0\">");
                                sw3.WriteLine("<generator class=\"identity\" />");
                                sw3.WriteLine("</id>");
                            }
                            else
                            {
                                switch (dr["col_typename"].ToString())
                                {
                                    case "int":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Int32\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "tinyint":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Int32\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "smallint":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Int32\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "bigint":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Int32\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "char":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "varchar":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "nchar":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "nvarchar":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" length=\"" + dr["col_len"].ToString() + "\" />"); break;
                                    case "datetime":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"DateTime\" />"); break;
                                    case "smalldatetime":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"DateTime\" />"); break;
                                    case "money":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Decimal\" />"); break;
                                    case "decimal":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Decimal\" />"); break;
                                    case "smallmoney":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Decimal\" />"); break;
                                    case "bit":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Boolean\" />"); break;
                                    case "uniqueidentifier":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"Guid\" length=\"30\" />"); break;
                                    case "text":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" />"); break;
                                    case "ntext":
                                        sw3.WriteLine("<property name=\"" + dr["col_name"].ToString() + "\" column=\"" + dr["col_name"].ToString() + "\" type=\"String\" />"); break;
                                }
                            }
                        }

                        sw3.WriteLine("</class>");
                        sw3.WriteLine("");
                        sw3.WriteLine("</hibernate-mapping>");
                        sw3.Flush();
                        sw3.Close();
                        sw3.Dispose();
                    }
                }

                Progress("Config files generated", 100);

                #endregion                

                MessageBox.Show("Generating process completed " + status + " errors.", "Generate Files", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                Progress(ex.Message, 0);
                MessageBox.Show(ex.Message, "Generate Files", MessageBoxButtons.OK, MessageBoxIcon.Error);
                status = "with";
            }
            finally
            {
                Progress("Generating process completed " + status + " errors.", 100);                
                this.Cursor = Cursors.Default;
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            System.Threading.Thread t = new System.Threading.Thread((new System.Threading.ThreadStart(Generate)));
            t.Start();
        }
   
        public void Progress(string progress, int value)
        {
            lblStatus.Text = progress + "...";
            if(value > 100)
            {ProgressBar.Value = 100;toolStripProgressBar1.Value =100;}
            else if(value == 0)
            {}
            else
            {ProgressBar.Value = value;toolStripProgressBar1.Value = value;}
            lboxProcesses.Items.Add(progress + "...");
            lboxProcesses.SelectedIndex = lboxProcesses.Items.Count - 1;
            System.Threading.Thread.Sleep(200);
        }

        private void toolStripLabel1_Click_1(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            new AboutBox().ShowDialog();
        }

        private void btnCheckAll_Click(object sender, EventArgs e)
        {
            for (int i = 0; i <= chklistTables.Items.Count - 1; i++)
            {
                chklistTables.SetItemChecked(i, true);
            }
        }

        private void btnUnCheckAll_Click(object sender, EventArgs e)
        {
             for (int i = 0; i <= chklistTables.Items.Count - 1; i++)
            {
                chklistTables.SetItemChecked(i, false);
            }
        }

        private void btnBrowseProjectFolder_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog().Equals(DialogResult.OK) && (!fbd.SelectedPath.Equals(null)))
            {
                txtProjectFolder.Text = fbd.SelectedPath;
            }
        }

        #region Generators

        private void VBNet_MsSql()
        {
            #region Write Classes

                Progress("Persistent classes generating", 35);
                for (int i = 0; i <= chklistTables.Items.Count - 1; i++)
                {
                    if (chklistTables.GetItemCheckState(i) == CheckState.Checked)
                    {
                        string tableName = chklistTables.Items[i].ToString();
                        System.IO.StreamWriter sw2 = new System.IO.StreamWriter(txtProjectFolder.Text + @"\App_Code\" + tableName + ".vb");
                        sw2.WriteLine("Imports System");
                        sw2.WriteLine("");
                        sw2.WriteLine("Namespace " + txtDefaultNamespace.Text);
                        sw2.WriteLine("");
                        sw2.WriteLine("Public Class " + tableName);
                        sw2.WriteLine("");

                        Progress("Capturing table columns schema", 0);
                        DataTable dt = dataClass.GetTableColumnsInfo("EXEC dbo.sp_MShelpcolumns '" + tableName + "'");

                        Progress("Writing fields for Class " + tableName , 0);
                        #region Writing Fields
                        foreach (DataRow dr in dt.Rows)
                        {
                            switch (dr["col_typename"].ToString())
                            {
                                case "int": sw2.WriteLine("Protected " + " _" + dr["col_name"].ToString() + " As Integer"); break;
                                case "tinyint": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Integer"); break;
                                case "smallint": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Integer"); break;
                                case "bigint": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Integer"); break;
                                case "char": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                                case "varchar": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                                case "nchar": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                                case "nvarchar": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                                case "datetime": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Date"); break;
                                case "smalldatetime": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Date"); break;
                                case "money": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Decimal"); break;
                                case "decimal": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Decimal"); break;
                                case "smallmoney": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Decimal"); break;
                                case "bit": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Boolean"); break;
                                case "uniqueidentifier": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As Guid"); break;
                                case "text": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                                case "ntext": sw2.WriteLine("Protected " + "_" + dr["col_name"].ToString() + " As String"); break;
                            }

                        }
                        #endregion
                        Progress("Fields applied for Class " + tableName, 0);

                        sw2.WriteLine("");
                        sw2.WriteLine("");

                        Progress("Writing properties for Class " + tableName, 0);
                        #region Writing Properties
                        foreach (DataRow dr in dt.Rows)
                        {
                            string typeValue = "";
                            switch (dr["col_typename"].ToString())
                            {
                                case "int": typeValue = "Integer"; break;
                                case "tinyint": typeValue = "Integer"; break;
                                case "smallint": typeValue = "Integer"; break;
                                case "bigint": typeValue = "Integer"; break;
                                case "char": typeValue = "String"; break;
                                case "varchar": typeValue = "String"; break;
                                case "nchar": typeValue = "String"; break;
                                case "nvarchar": typeValue = "String"; break;
                                case "datetime": typeValue = "Date"; break;
                                case "smalldatetime": typeValue = "Date"; break;
                                case "money": typeValue = "Decimal"; break;
                                case "decimal": typeValue = "Decimal"; break;
                                case "smallmoney": typeValue = "Decimal"; break;
                                case "bit": typeValue = "Boolean"; break;
                                case "uniqueidentifier": typeValue = "Guid"; break;
                                case "text": typeValue = "String"; break;
                                case "ntext": typeValue = "String"; break;
                            }
                            sw2.WriteLine("Public Property " + dr["col_name"].ToString() + "() As " + typeValue); 
                            sw2.WriteLine("Get");
                            sw2.WriteLine("Return " + "_" + dr["col_name"].ToString());
                            sw2.WriteLine("End Get");
                            sw2.WriteLine("Set(ByVal value As " + typeValue + ")");
                            sw2.WriteLine(dr["col_name"].ToString() + " = value");
                            sw2.WriteLine("End Set");
                            sw2.WriteLine("End Property");
                            sw2.WriteLine("");
                        }
                        #endregion
                        Progress("Properties applied for Class " + tableName, 0);

                         sw2.WriteLine("End Class");
                         sw2.WriteLine("End Namespace");
                        sw2.Flush();
                        sw2.Close();
                        sw2.Dispose();
                        Progress("Class file " + tableName + " generated" , 0);
                    }
                }

                Progress("Persistent classes generated", 60);

                #endregion      
        }


        private void CSharp_MsSql()
        {
            #region Write Classes

                Progress("Persistent classes generating", 35);
                for (int i = 0; i <= chklistTables.Items.Count - 1; i++)
                {
                    if (chklistTables.GetItemCheckState(i) == CheckState.Checked)
                    {
                        string tableName = chklistTables.Items[i].ToString();
                        System.IO.StreamWriter sw2 = new System.IO.StreamWriter(txtProjectFolder.Text + @"\App_Code\" + tableName + ".cs");
                        sw2.WriteLine("using System;");
                        sw2.WriteLine("");
                        sw2.WriteLine("namespace " + txtDefaultNamespace.Text);
                        sw2.WriteLine("{");
                        sw2.WriteLine("public class " + tableName);
                        sw2.WriteLine("{");

                        Progress("Capturing table columns schema", 0);
                        DataTable dt = dataClass.GetTableColumnsInfo("EXEC dbo.sp_MShelpcolumns '" + tableName + "'");

                        Progress("Writing fields for Class " + tableName , 0);
                        #region Writing Fields
                        foreach (DataRow dr in dt.Rows)
                        {
                            switch (dr["col_typename"].ToString())
                            {
                                case "int": sw2.WriteLine("private " + "int" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "tinyint": sw2.WriteLine("private " + "int" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "smallint": sw2.WriteLine("private " + "int" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "bigint": sw2.WriteLine("private " + "int" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "char": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "varchar": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "nchar": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "nvarchar": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "datetime": sw2.WriteLine("private " + "DateTime" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "smalldatetime": sw2.WriteLine("private " + "DateTime" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "money": sw2.WriteLine("private " + "decimal" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "decimal": sw2.WriteLine("private " + "decimal" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "smallmoney": sw2.WriteLine("private " + "decimal" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "bit": sw2.WriteLine("private " + "bool" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "uniqueidentifier": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "text": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                                case "ntext": sw2.WriteLine("private " + "string" + " _" + dr["col_name"].ToString() + ";"); break;
                            }

                        }
                        #endregion
                        Progress("Fields applied for Class " + tableName, 0);

                        sw2.WriteLine("");
                        sw2.WriteLine("");

                        Progress("Writing properties for Class " + tableName, 0);
                        #region Writing Properties
                        foreach (DataRow dr in dt.Rows)
                        {
                            switch (dr["col_typename"].ToString())
                            {
                                case "int": sw2.WriteLine("public " + "int " + dr["col_name"].ToString()); break;
                                case "tinyint": sw2.WriteLine("public " + "int " + dr["col_name"].ToString()); break;
                                case "smallint": sw2.WriteLine("public " + "int " + dr["col_name"].ToString()); break;
                                case "bigint": sw2.WriteLine("public " + "int " + dr["col_name"].ToString()); break;
                                case "char": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "varchar": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "nchar": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "nvarchar": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "datetime": sw2.WriteLine("public " + "DateTime " + dr["col_name"].ToString()); break;
                                case "smalldatetime": sw2.WriteLine("public " + "DateTime " + dr["col_name"].ToString()); break;
                                case "money": sw2.WriteLine("public " + "decimal " + dr["col_name"].ToString()); break;
                                case "decimal": sw2.WriteLine("public " + "decimal " + dr["col_name"].ToString()); break;
                                case "smallmoney": sw2.WriteLine("public " + "decimal " + dr["col_name"].ToString()); break;
                                case "bit": sw2.WriteLine("public " + "bool " + dr["col_name"].ToString()); break;
                                case "uniqueidentifier": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "text": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                                case "ntext": sw2.WriteLine("public " + "string " + dr["col_name"].ToString()); break;
                            }
                            sw2.WriteLine("{");
                            sw2.WriteLine("get { return _" + dr["col_name"].ToString() + "; }");
                            sw2.WriteLine("set { _" + dr["col_name"].ToString() + " = value; }");
                            sw2.WriteLine("}");
                            sw2.WriteLine("");
                        }
                        #endregion
                        Progress("Properties applied for Class " + tableName, 0);

                        sw2.WriteLine("");
                        sw2.WriteLine("}");
                        sw2.WriteLine("}");
                        sw2.Flush();
                        sw2.Close();
                        sw2.Dispose();
                        Progress("Class file " + tableName + " generated" , 0);
                    }
                }

                Progress("Persistent classes generated", 60);

                #endregion      
        }

        #endregion




    }
}